/***************************************************************************************************
Author: Arun Ramachandran
Version: 1.0
Project Name : OOR Phase 2
Purpose: To delete the AF Assigned History object records older than 400 days.
Update: [Arun [1/9]] - Updated API version from 23.0 to 22.0
Update: [Arun [1/10]] -  Added Inline comments in the code.
                        Updated sObject loop with meaningful variable names.
                        
Update: [Arun[2/17]] - Updated the test class to improve coverage of the Batchable class.                        

****************************************************************************************************/


global class OORDeleteRecords implements Database.Batchable<sObject>{
    public   String query;
    
    global database.querylocator start(Database.BatchableContext BC){
          return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<sObject> scope){
        
            list<AF_Assigned_History__c> DelRec = new list<AF_Assigned_History__c>(); // List for deleting the records
            
            for(sObject RecordDel : scope){ 
            AF_Assigned_History__c l= (AF_Assigned_History__c)RecordDel;                
            DelRec.add(l);   // Adding records to be deleted in a List [Arun [1/10]]
            }
            
            if(DelRec.size()>0){
            
                Try{            
                        Delete DelRec;   //Deleting Records which are older than 400 days old [Arun [1/10]]
                   }
                    Catch(DMLException e){
                                     system.debug('DML Exception'+e);
                                 }            
           }
    }
    global void finish(Database.BatchableContext BC){
  }
  static testMethod void testOORDeleteRecords(){
     
     //Update added by Arun Ramachandran on 2/17 to improve coverage
      Test.startTest();
      OORDeleteRecords job = new OORDeleteRecords();
      AF_Assigned_History__c AF3 = new AF_Assigned_History__c(Name = 'Testing 3', Ready_To_Delete__c = True);
      Insert AF3; 
      job.query = 'Select Id from AF_Assigned_History__c where Ready_to_Delete__c = True';
      Database.ExecuteBatch(job,100);
      Test.stopTest();
  }

}